import {
  _decorator,
  Animation,
  AudioClip,
  Collider2D,
  Component,
  Contact2DType,
  EventTouch,
  Input,
  input,
  instantiate,
  IPhysics2DContact,
  Node,
  Prefab,
  Sprite,
  Vec3,
  sp,
  quat,
  Quat,
  v3,
} from "cc";
import { Reward, RewardType } from "./Reward";
import { Reward2, RewardType2 } from "./Reward2";
import { GameManager } from "./GameManager";
import { LifeCountUI } from "./UI/LifeCountUI";
import { AudioMgr } from "./AudioMgr";
import { fanBullet } from "./fanBullet";
import { Bullet2 } from "./Bullet2";
const { ccclass, property } = _decorator;

enum ShootType {
  None,
  OneShoot,
  TwoShoot,
  FanShoot,
}

@ccclass("Player")
export class Player extends Component {
  @property
  shootRate: number = 0.7;
  @property(Node)
  bulletParent: Node = null;
  @property(Prefab)
  bullet1Prefab: Prefab = null;
  @property(Node)
  position1: Node = null;

  @property(Prefab)
  bullet2Prefab: Prefab = null;
  @property(Prefab)
  bullet3Prefab: Prefab = null;
  @property(Node)
  position2: Node = null;
  @property(Node)
  position3: Node = null;

  @property
  shootType: ShootType = ShootType.OneShoot;

  @property
  lifeCount: number = 3;
  // @property(Animation)
  // anim: Animation = null;

  @property
  animHit: string = "";
  @property
  animDown: string = "";

  @property
  invincibleTime: number = 1;

  @property
  twoShootTime: number = 5;
  @property
  fanAngle: number = 60; // 扇形角度
  @property
  fanBullets: number = 2; // 子弹数量
  @property
  fanDuration: number = 5; // 持续时间

  @property(LifeCountUI)
  lifeCountUI: LifeCountUI = null;
  @property(AudioClip)
  bulletAudio: AudioClip = null;
  @property(AudioClip)
  getBombAudio: AudioClip = null;
  @property(AudioClip)
  getDoubleAudio: AudioClip = null;
  @property(sp.Skeleton)
  skeleton: sp.Skeleton = null;

  twoShootTimer: number = 0;
  isInvincible: boolean = false;
  invincibleTimer: number = 0;
  shootTimer: number = 0;
  fanShootTimer: number = 0;
  fanBulletsNumber: number = 0;

  collider: Collider2D = null;
  private canControl: boolean = true;

  protected onLoad(): void {
    input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);

    // 注册单个碰撞体的回调函数
    this.collider = this.getComponent(Collider2D);
    if (this.collider) {
      this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }
  }
  protected start(): void {
    this.lifeCountUI.updateUI(this.lifeCount);
  }
  protected onDestroy(): void {
    input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
    if (this.collider) {
      this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }
  }

  onBeginContact(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    const reward =
      otherCollider.getComponent(Reward) || otherCollider.getComponent(Reward2);
    if (reward) {
      this.onContactToReward(reward);
    } else {
      this.onContactToEnemy();
    }
  }

  transitionToTwoShoot() {
    this.shootType = ShootType.TwoShoot;
    this.twoShootTimer = 0;
  }
  transitionToOneShoot() {
    this.shootType = ShootType.OneShoot;
  }

  lastReward: Reward | Reward2 = null;

  onContactToReward(reward: Reward | Reward2) {
    if (reward == this.lastReward) {
      return;
    }
    this.lastReward = reward;
    if (reward instanceof Reward2) {
      switch (reward.rewardType) {
        case RewardType2.Damage:
          Bullet2.damage += 1;
          break;
        case RewardType2.BulletSpeed:
          Bullet2.speed += 10;
          break;
      }
    } else {
      switch (reward.rewardType) {
        case RewardType.TwoShoot:
          AudioMgr.inst.playOneShot(this.getDoubleAudio);
          this.transitionToTwoShoot();
          break;
        case RewardType.Bomb:
          AudioMgr.inst.playOneShot(this.getBombAudio);
          GameManager.getInstance().AddBomb();
          break;
        case RewardType.FanShoot:
          AudioMgr.inst.playOneShot(this.getDoubleAudio);
          if (this.fanBulletsNumber < 2) {
            this.fanBulletsNumber += 1;
          }
          this.transitionToFanShoot();
          break;
      }
    }

    reward.getComponent(Sprite).enabled = false;
    reward.getComponent(Collider2D).enabled = false;
  }
  onContactToEnemy() {
    if (this.isInvincible) return;

    this.isInvincible = true;
    this.invincibleTimer = 0;

    this.changeLifeCount(-1);
    if (this.lifeCount > 0) {
      //   this.anim.play(this.animHit);
      this.skeleton.setAnimation(0, this.animHit, false);
    } else {
      //   this.anim.play(this.animDown);
      this.skeleton.setAnimation(0, this.animDown, false);
    }

    if (this.lifeCount <= 0) {
      this.shootType = ShootType.None;
      if (this.collider) {
        this.collider.enabled = false;
      }
      this.scheduleOnce(() => {
        GameManager.getInstance().gameOver();
      }, 0.4);
    }
  }

  changeLifeCount(count: number) {
    this.lifeCount += count;
    this.lifeCountUI.updateUI(this.lifeCount);
  }

  onTouchMove(event: EventTouch) {
    if (this.canControl == false) return;
    if (this.lifeCount < 1) return;

    const p = this.node.position;

    let targetPosition = new Vec3(
      p.x + event.getDeltaX(),
      p.y + event.getDeltaY(),
      p.z
    );

    if (targetPosition.x < -230) {
      targetPosition.x = -230;
    }
    if (targetPosition.x > 230) {
      targetPosition.x = 230;
    }
    if (targetPosition.y > 380) {
      targetPosition.y = 380;
    }
    if (targetPosition.y < -380) {
      targetPosition.y = -380;
    }

    this.node.setPosition(targetPosition);
  }

  protected update(dt: number): void {
    switch (this.shootType) {
      case ShootType.OneShoot:
        this.oneShoot(dt);
        break;
      case ShootType.TwoShoot:
        this.twoShoot(dt);
        break;
      case ShootType.FanShoot: // 新增扇形处理
        this.fanShoot(dt);
        break;
    }

    if (this.isInvincible) {
      this.invincibleTimer += dt;
      if (this.invincibleTimer > this.invincibleTime) {
        this.isInvincible = false;
      }
    }
  }

  oneShoot(dt: number) {
    this.shootTimer += dt;
    if (this.shootTimer >= this.shootRate) {
      AudioMgr.inst.playOneShot(this.bulletAudio, 0.1);
      this.shootTimer = 0;
      const bullet1 = instantiate(this.bullet1Prefab);
      this.bulletParent.addChild(bullet1);
      bullet1.setWorldPosition(this.position1.worldPosition);
    }
  }
  twoShoot(dt: number) {
    this.twoShootTimer += dt;
    if (this.twoShootTimer > this.twoShootTime) {
      this.transitionToOneShoot();
    }

    this.shootTimer += dt;
    if (this.shootTimer >= this.shootRate) {
      AudioMgr.inst.playOneShot(this.bulletAudio, 0.1);
      this.shootTimer = 0;
      const bullet1 = instantiate(this.bullet2Prefab);
      const bullet2 = instantiate(this.bullet2Prefab);
      this.bulletParent.addChild(bullet1);
      this.bulletParent.addChild(bullet2);
      bullet1.setWorldPosition(this.position2.worldPosition);
      bullet2.setWorldPosition(this.position3.worldPosition);
    }
  }
  // 新增扇形射击方法
  private fanShoot(dt: number) {
    // this.fanShootTimer += dt;
    // if (this.fanShootTimer > this.fanDuration) {
    //   this.transitionToOneShoot();
    // }
    this.shootTimer += dt;
    if (this.shootTimer >= this.shootRate) {
      AudioMgr.inst.playOneShot(this.bulletAudio, 0.1);
      this.shootTimer = 0;
      const currentAngle = 90 - 5;
      const radians = (currentAngle * Math.PI) / 180;
      let dirs: Array<Vec3> = [];
      // if (this.fanBullets == 2) {
      //   this.fanBullets = 3;
      // }
      this.fanBullets = this.fanBulletsNumber + 1;
      if (this.fanBullets === 3) {
        dirs = [
          v3(Math.cos(radians), Math.sin(radians), 0),
          Vec3.UP,
          v3(-Math.cos(radians), Math.sin(radians), 0),
        ];
      } else {
        dirs = [
          v3(Math.cos(radians), Math.sin(radians), 0),
          v3(-Math.cos(radians), Math.sin(radians), 0),
        ];
      }
      for (let index = 0; index < this.fanBullets; index++) {
        // 确保预制体有效
        if (!this.bullet3Prefab) {
          console.error("bullet3Prefab is not assigned!");
          return;
        }

        const bullet: Node = instantiate(this.bullet3Prefab);
        // 检查父节点有效性
        if (!this.bulletParent) {
          console.error("bulletParent is not assigned!");
          bullet.destroy();
          return;
        }
        // 先设置父节点再操作组件
        bullet.setParent(this.bulletParent);
        bullet.active = true; // 确保节点激活
        bullet.setWorldPosition(this.position1.worldPosition);
        const bulletComp = bullet.getComponent("fanBullet") as fanBullet;
        if (bulletComp) {
          const newDir = new Vec3(dirs[index]);
          bulletComp.direction = newDir.normalize();
        }
      }
    }
  }
  // 新增状态切换方法
  private transitionToFanShoot() {
    this.fanShootTimer = 0;
    this.shootType = ShootType.FanShoot;
  }
  disableControl() {
    this.canControl = false;
  }
  enableControl() {
    this.canControl = true;
  }
}
